using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Blople.GameComponents
{
    public class Logo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected SpriteBatch sBatch;

        protected Texture2D texture;

        protected Vector2 position;
        protected float direction;

        protected const int WIDTH = 300;
        protected const int HEIGHT = 100;

        protected List<Rectangle> frames;
        private int activeFrame;

        int lastFrameTime;
        const int TIME_BETWEEN_FRAMES = 2500;

        public Logo(Game game, Texture2D texture) : base(game)
        {
            this.texture = texture;
            direction = 0.0f;
            position = new Vector2(250, 75);
            frames = new List<Rectangle>();
            activeFrame = 0;
            CreateFrames();
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (System.Environment.TickCount - lastFrameTime > TIME_BETWEEN_FRAMES)
            {
                activeFrame = (activeFrame + 1) % frames.Count;
                lastFrameTime = System.Environment.TickCount;
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            sBatch.Draw(texture, position, frames[activeFrame], Color.White, direction, new Vector2(WIDTH / 2, HEIGHT / 2), 1.0f, SpriteEffects.None, 0.3f);
            base.Draw(gameTime);
        }

        public void CreateFrames()
        {
            for (int i = 0; i < 4; i++)
            {
                frames.Add(new Rectangle(0, i * HEIGHT, WIDTH, HEIGHT));
            }
        }
    }
}
